//
//  RCMainTabBar.swift
//  RC-微博0305
//
//  Created by 任淳 on 16/3/6.
//  Copyright © 2016年 ReverChun. All rights reserved.
//

import UIKit

class RCMainTabBar: UITabBar {

    
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let x:CGFloat = 0
        let y:CGFloat = 0
        let w = UIScreen.mainScreen().bounds.width / 5
        let h = self.bounds.height
        
        let rect = CGRectMake(x, y, w, h)
        
        var index:CGFloat = 0
        for subView in subviews {
            //要判断subView的类型是不是tabBatItem 有的不是 不进行操作
            
            if subView.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                
                subView.frame = CGRectOffset(rect, w * index, 0)
                
                //空出中间添加按钮的位置
                if index == 1 {
                    
                    index++
                }
                index++
                
            }
        }
        
        addBtn.frame = CGRectMake(w * 2, 0, w, self.bounds.height)
        addSubview(addBtn)
    }
    
    //addBtn的点击事件
    @objc private func addBtnDidPush() {
        print("点了添加按钮")
        
    }
    
    //懒加载创建addBtn
    lazy var addBtn:UIButton = {
        
        let addBtn = UIButton()
        addBtn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: .Normal)
        addBtn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: .Selected)
        addBtn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState:.Normal)
        addBtn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: .Selected)
        
//        addBtn.frame = CGRectMake((UIScreen.mainScreen().bounds.width / 5 * 2), 0, UIScreen.mainScreen().bounds.width / 5, 44)
        
        addBtn.addTarget(self , action: "addBtnDidPush", forControlEvents: .TouchUpInside)
        
        return addBtn
    }()
    

}
